﻿using System;
using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class ErrorLogService : IErrorLogService
        {
                private readonly IErrorLogCollect _ErrorLog;
                private readonly IServerTypeCollect _ServerType;
                private readonly IServerCollect _Server;

                public ErrorLogService (IErrorLogCollect errorLog,
                        IServerCollect server,
                        IServerTypeCollect serverType)
                {
                        this._Server = server;
                        this._ErrorLog = errorLog;
                        this._ServerType = serverType;
                }

                public SystemErrorLog Get (Guid id)
                {
                        return this._ErrorLog.Get (id);
                }

                public SystemLog[] Query (LogQueryParam query, IBasicPage paging, out long count)
                {
                        SysErrorLog[] logs = this._ErrorLog.Query (query, paging, out count);
                        if (logs.Length == 0)
                        {
                                return null;
                        }
                        BasicServerType[] types = this._ServerType.GetBasic (logs.Distinct (a => a.SystemType));
                        BasicService[] servers = this._Server.GetBasic (logs.Distinct (a => a.ServerId));
                        return logs.ConvertMap<SysErrorLog, SystemLog> ((a, b) =>
                        {
                                b.ServerName = servers.Find (c => c.Id == a.ServerId, c => c.ServerName);
                                b.SystemType = types.Find (c => c.TypeVal == a.SystemType, c => c.SystemName);
                                return b;
                        });
                }
        }
}
